package com.chixing.ih.module.task.mapper.provider;

import com.chixing.ih.module.task.pojo.Order;
import com.chixing.ih.status.IsDeleteEnum;
import com.chixing.ih.status.OrderEnum;
import com.chixing.ih.util.EmptyUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/9/25 23:03
 */
public class OrderMapperProvider {

    public String insertOne() {
        return new SQL() {{
            INSERT_INTO("tbl_order");
            INTO_COLUMNS("order_num", "send_task_user_id", "receive_task_user_id", "task_id", "salary",
                    "order_begin_time", "order_end_time","update_time");
            INTO_VALUES("#{orderNum}", "#{sendTaskUserId}", "#{receiveTaskUserId}", "#{taskId}",
                    "#{salary}", "#{orderBeginTime}", "#{orderEndTime}","#{updateTime}");
        }}.toString();
    }

    public String delOne() {
        return new SQL() {{
            DELETE_FROM("tbl_order");
            WHERE("order_id = #{orderId}");
        }}.toString();
    }

    public String deleteAll() {
        return new SQL() {{
            DELETE_FROM("tbl_order");
            WHERE("send_task_user_id = #{userId}");
        }}.toString();
    }

    public String updateOne(Order order) {
        return new SQL() {{
            UPDATE("tbl_order");
            if (!EmptyUtil.isEmpty(order.getOrderNum())) {
                SET("order_num = #{orderNum}");
            }
            if (!EmptyUtil.isEmpty(order.getSendTaskUserId())) {
                SET("send_task_user_id = #{sendTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getReceiveTaskUserId())) {
                SET("receive_task_user_id = #{receiveTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getTaskId())) {
                SET("task_id = #{taskId}");
            }
            if (!EmptyUtil.isEmpty(order.getSalary())) {
                SET("salary = #{salary}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderBeginTime())) {
                SET("order_begin_time = #{orderBeginTime}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderEndTime())) {
                SET("order_end_time = #{orderEndTime}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderStatus())) {
                SET("order_status = #{orderStatus}");
            }
            if (!EmptyUtil.isEmpty(order.getLogicalDeletion())) {
                SET("is_deleted = #{logicalDeletion}");
            }
            if (!EmptyUtil.isEmpty(order.getCreateTime())) {
                SET("create_time = #{createTime}");
            }
            if (!EmptyUtil.isEmpty(order.getUpdateTime())) {
                SET("update_time = #{updateTime}");
            }
            if (!EmptyUtil.isEmpty(order.getLogicalDeletion())) {
                SET("is_deleted =#{value}");
            }
            if (!EmptyUtil.isEmpty(order.getPaymentStatus())) {
                SET("payment_status = #{paymentStatus}");
            }
            WHERE("order_id = #{orderId}");
        }}.toString();
    }

    public String updateOrder(Order order) {
        return new SQL() {{
            UPDATE("tbl_order");
            if (!EmptyUtil.isEmpty(order.getSendTaskUserId())) {
                SET("send_task_user_id = #{sendTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getReceiveTaskUserId())) {
                SET("receive_task_user_id = #{receiveTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getTaskId())) {
                SET("task_id = #{taskId}");
            }
            if (!EmptyUtil.isEmpty(order.getSalary())) {
                SET("salary = #{salary}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderBeginTime())) {
                SET("order_begin_time = #{orderBeginTime}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderEndTime())) {
                SET("order_end_time = #{orderEndTime}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderStatus())) {
                SET("order_status = #{orderStatus}");
            }
            if (!EmptyUtil.isEmpty(order.getLogicalDeletion())) {
                SET("is_deleted = #{logicalDeletion}");
            }
            if (!EmptyUtil.isEmpty(order.getCreateTime())) {
                SET("create_time = #{createTime}");
            }
            if (!EmptyUtil.isEmpty(order.getUpdateTime())) {
                SET("update_time = #{updateTime}");
            }
            if (!EmptyUtil.isEmpty(order.getLogicalDeletion())) {
                SET("is_deleted =#{value}");
            }
            if (!EmptyUtil.isEmpty(order.getPaymentStatus())) {
                SET("payment_status = #{paymentStatus}");
            }
            WHERE("order_num = #{orderNum}");
        }}.toString();
    }

    public String getOne() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_order");
            WHERE("order_id = #{orderId}");
        }}.toString();
    }

    public String getPendingComOrder() {
        return new SQL() {{
            SELECT("o.send_task_user_id AS sendId,o.receive_task_user_id AS receiveId,u.nick_name AS receiveName,i.image_url AS imageUrl,u.user_career AS career,t.task_title AS taskTitle,t.salary");
            FROM("tbl_order o JOIN tbl_user u ON o.receive_task_user_id=u.user_id JOIN tbl_user_image i ON u.user_id=i.user_id JOIN tbl_task t ON o.task_id=t.id");
            WHERE("o.order_id = #{orderId}");
        }}.toString();
    }


    public String findOne() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_order");
            WHERE("task_id = #{taskId}");
        }}.toString();
    }

    public String findOrders(Order order) {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_order");
            if (!EmptyUtil.isEmpty(order.getSendTaskUserId())) {
                WHERE("send_task_user_id=#{sendTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getReceiveTaskUserId())) {
                AND().WHERE("receive_task_user_id=#{receiveTaskUserId}");
            }
            if (!EmptyUtil.isEmpty(order.getOrderStatus())) {
                AND().WHERE("order_status=#{orderStatus}");
            }
        }}.toString();
    }

    public String findAll() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_order");
        }}.toString();
    }

    public String findCompletedTaskSuccess() {
        return new SQL() {{
            SELECT("task_id");
            FROM("tbl_order");
            WHERE("receive_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.TWO.status);
        }}.toString();
    }


    public String findCompletedTaskFailure() {
        return new SQL() {{
            SELECT("task_id");
            FROM("tbl_order");
            WHERE("receive_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.FOUR.status);
        }}.toString();
    }



    public String findCompletedEmploymentSuccess() {
        return new SQL() {{
            SELECT("order_id,task_id,payment_status,u.nick_name AS receiveName,i.image_url AS receiveImageUrl");
            FROM("tbl_order o JOIN tbl_user u ON o.receive_task_user_id = u.user_id JOIN tbl_user_image i ON u.user_id = i.user_id");
            WHERE("send_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.TWO.status);
        }}.toString();
    }


    public String findCompletedEmploymentFailure() {
        return new SQL() {{
            SELECT("order_id,task_id,payment_status,u.nick_name AS receiveName,i.image_url AS receiveImageUrl");
            FROM("tbl_order o JOIN tbl_user u ON o.receive_task_user_id = u.user_id JOIN tbl_user_image i ON u.user_id = i.user_id");
            WHERE("send_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.FOUR.status);
        }}.toString();
    }

    public String findProgressTask() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_order");
            WHERE("receive_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.ZERO.status);
        }}.toString();
    }


    public String findProgressEmployment() {
        return new SQL() {{
            SELECT("order_id AS orderId,task_id AS id,t.task_title AS mandateTitle,t.salary,t.work_cycle AS workCycle,payment_status AS paymentStatus,u.nick_name AS receiveName,i.image_url AS receiveImageUrl");
            FROM("tbl_order o JOIN tbl_task t ON o.task_id=t.id JOIN tbl_user u ON o.receive_task_user_id = u.user_id JOIN tbl_user_image i ON u.user_id = i.user_id");
            WHERE("send_task_user_id = #{userId}");
            AND().WHERE("order_status = " + OrderEnum.ZERO.status);
        }}.toString();
    }

    public String orderTotalNumber() {
        return new SQL() {{
            SELECT("count(1)");
            FROM("tbl_order");
        }}.toString();
    }

    public String getOneOrder() {
        return new SQL() {{
            SELECT("o.order_num AS orderNum,t.task_title AS taskTitle,t.publishing_name AS publishingName,t.salary AS salary,t.work_cycle workCycle,u.nick_name AS receiveTaskUser,publishing_phone AS publishingPhone,o.order_begin_time AS beginTime");
            FROM("tbl_order o JOIN tbl_task t ON o.task_id=t.id JOIN tbl_user u ON o.receive_task_user_id=u.user_id");
            WHERE("o.order_id= #{order} AND o.is_deleted=0");
        }}.toString();
    }


}
